define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init();
            
            //绑定事件
            $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
                var panel = $($(this).attr("href"));
                if (panel.size() > 0) {
                    Controller.table[panel.attr("id")].call(this);
                    $(this).on('click', function (e) {
                        $($(this).attr("href")).find(".btn-refresh").trigger("click");
                    });
                }
                //移除绑定的事件
                $(this).unbind('shown.bs.tab');
            });
            
            //必须默认触发shown.bs.tab事件
            $('ul.nav-tabs li.active a[data-toggle="tab"]').trigger("shown.bs.tab");

            var table1 = $("#table1");
            // 启动和暂停按钮
            $(document).on("click", ".btn-start,.btn-pause", function () {
                //在table外不可以使用添加.btn-change的方法
                //只能自己调用Table.api.multi实现
                //如果操作全部则ids可以置为空
                var ids = Table.api.selectedids(table1);
                Table.api.multi("changestatus", ids.join(","), table1, this);
            });
            //selecttype 切换redio的id 点击切换改变对应的参数                
            $("#copy_goods").data("params", function (obj) {             
                var type = $("#c-type").find("option:selected").val();
                return type;   
            });
            $("#synchronous").data("params", function (obj) {             
                var type = $("#c-type").find("option:selected").val();
                return type;   
            });
            // 当表格数据加载完成时
            table1.on('load-success.bs.table', function (e, data) {
                //这里可以获取从服务端获取的JSON数据
                $("#number_goods").text(data.Statistics.number_goods);
                $("#been_updat").text(data.Statistics.been_updat);
                $("#not_updat").text(data.Statistics.not_updat);
                $("#update_error").text(data.Statistics.update_error);
            });
            Table.api.bindevent(table1);


            var table2 = $("#table2");
            // 启动和暂停按钮
            $(document).on("click", ".btn-start2,.btn-pause", function () {
                //在table外不可以使用添加.btn-change的方法
                //只能自己调用Table.api.multi实现
                //如果操作全部则ids可以置为空
                var ids = Table.api.selectedids(table2);
                Table.api.multi("changestatus", ids.join(","), table2, this);
            });
            //selecttype 切换redio的id 点击切换改变对应的参数
            $("#copy_goods2").data("params", function (obj) {             
                var type = $("#c-type2").find("option:selected").val();
                return type;   
            });
            $("#synchronous2").data("params", function (obj) {             
                var type = $("#c-type2").find("option:selected").val();
                return type;   
            });
            // 当表格数据加载完成时
            table2.on('load-success.bs.table', function (e, data) {
                //这里可以获取从服务端获取的JSON数据
                $("#number_goods2").text(data.Statistics.number_goods2);
                $("#been_updat2").text(data.Statistics.been_updat2);
                $("#not_updat2").text(data.Statistics.not_updat2);
                $("#update_error2").text(data.Statistics.update_error2);
            });
            Table.api.bindevent(table2);

        },

        table: {
            first: function () {
                // 表格1
                var table1 = $("#table1");
                table1.bootstrapTable({
                    url: 'products_shopify/index/?type=1',
                    toolbar: '#toolbar1',
                    pk: 'id',
                    sortName: 'updated_at',
                    searchFormVisible: true,
                    columns: [
                        [
                            {checkbox: true},
                            {field: 'accounturl', title: __('店铺名称'),visible: false,addClass: "selectpage", extend: "data-source='feditionaccounturl/index' data-max-select-limit='50' data-multiple='true'", operate: 'IN'},
                            {field: 'accounturl_name', title: __('店铺名称'), operate: false},
                            {field: 'title', title: __('商品名称（鼠标悬停查看完整信息）'), formatter:Table.api.formatter.flag, operate:'LIKE',cellStyle:function (value,row,index,field) {
                                return {
                                    css: {
                                        "min-width": "150px",
                                        "white-space": "nowrap",
                                        "text-overflow": "ellipsis",
                                        "overflow": "hidden",
                                        "max-width":"300px"
                                    }
                                };
                            },formatter:function (value,row,index,field) {
                                var span=document.createElement('span');
                                span.setAttribute('title',value);
                                span.innerHTML = value;
                                return span.outerHTML;
                            }},
                            // {field: 'vendor', title: __('Vendor'), operate: 'LIKE'},
                            {field: 'product_type', title: __('Product_type'), operate: 'LIKE'},
                            {field: 'updated_at', title: __('Updated_at'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                            
                            {field: 'updatestatus', title: ('SKU状态'), searchList: {"normal":__('已更新'),"hidden":__('未更新'),"unavailable":__('更新错误'),"needless":__('无需更新')},defaultValue:"hidden", formatter: Table.api.formatter.status},

                            // {field: 'updated_at', title: __('Updated_at'), operate: 'LIKE'},
                            // {field: 'tags', title: __('Tags'), operate: 'LIKE'},

                            {field: 'skus', title: __('SKU（鼠标悬停查看完整信息）'), formatter:Table.api.formatter.flag, operate:false,cellStyle:function (value,row,index,field) {
                                return {
                                    css: {
                                        "min-width": "150px",
                                        "white-space": "nowrap",
                                        "text-overflow": "ellipsis",
                                        "overflow": "hidden",
                                        "max-width":"300px"
                                    }
                                };
                            },formatter:function (value,row,index,field) {
                                var span=document.createElement('span');
                                span.setAttribute('title',value);
                                span.innerHTML = value;
                                return span.outerHTML;
                            }},
                            {field: 'id', title: __('商品ID'), operate: 'LIKE'},
                            {field: 'images', title: __('主图'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.images},
                            // {field: 'options', title: __('Options'), operate: 'LIKE'},
                            // {field: 'status', title: __('Status'), operate: 'LIKE', formatter: Table.api.formatter.status},

                            // {
                            //     field: 'buttons',
                            //     width: "120px",
                            //     title: __('修改SKU'),
                            //     table: table1,
                            //     events: Table.api.events.operate,
                            //     operate: false,
                            //     buttons: [
                            //         {
                            //             name: 'detail',
                            //             text: __('修改SKU'),
                            //             title: __('修改SKU'),
                            //             extend: 'data-area=\'["90%","90%"]\'',
                            //             classname: 'btn btn-sm btn-warning btn-dialog',
                            //             icon: 'fa fa-spotify',
                            //             url: 'products_shopify_variants/index'
                            //         },
                            //     ],
                            //     formatter: Table.api.formatter.buttons
                            // },

                            // {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                        ]
                    ]
                });
                // 为表格1绑定事件
                Table.api.bindevent(table1);
            },
            second: function () {
                // 表格2
                var table2 = $("#table2");
                table2.bootstrapTable({
                    url: 'products_shopify/index/?type=2',
                    extend: {
                        index_url: 'products_shopify/index/?type=2',
                        add_url: 'products_shopify/add',
                        edit_url: 'products_shopify/edit',
                        del_url: 'products_shopify/del',
                        multi_url: 'products_shopify/multi',
                        import_url: 'products_shopify/import',
                        table: 'products_shopify',
                    },
                    toolbar: '#toolbar2',
                    sortName: 'updated_at',
                    searchFormVisible: true,
                    columns: [
                        [
                            {checkbox: true},
                            {field: 'accounturl', title: __('店铺名称'),visible: false,addClass: "selectpage", extend: "data-source='feditionaccounturl/index' data-max-select-limit='50' data-multiple='true'", operate: 'IN'},
                            {field: 'accounturl_name', title: __('店铺名称'), operate: false},
                            {field: 'title', title: __('商品名称（鼠标悬停查看完整信息）'), formatter:Table.api.formatter.flag, operate:'LIKE',cellStyle:function (value,row,index,field) {
                                return {
                                    css: {
                                        "min-width": "150px",
                                        "white-space": "nowrap",
                                        "text-overflow": "ellipsis",
                                        "overflow": "hidden",
                                        "max-width":"300px"
                                    }
                                };
                            },formatter:function (value,row,index,field) {
                                var span=document.createElement('span');
                                span.setAttribute('title',value);
                                span.innerHTML = value;
                                return span.outerHTML;
                            }},
                            // {field: 'vendor', title: __('Vendor'), operate: 'LIKE'},
                            {field: 'product_type', title: __('Product_type'), operate: 'LIKE'},
                            {field: 'updated_at', title: __('Updated_at'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                            {field: 'updatestatus', title: ('SKU状态'), searchList: {"normal":__('已更新'),"hidden":__('未更新'),"unavailable":__('更新错误'),"needless":__('无需更新')},defaultValue:"hidden", formatter: Table.api.formatter.status},
                            {field: 'skus', title: __('SKU（鼠标悬停查看完整信息）'), formatter:Table.api.formatter.flag, operate:false,cellStyle:function (value,row,index,field) {
                                return {
                                    css: {
                                        "min-width": "150px",
                                        "white-space": "nowrap",
                                        "text-overflow": "ellipsis",
                                        "overflow": "hidden",
                                        "max-width":"300px"
                                    }
                                };
                            },formatter:function (value,row,index,field) {
                                var span=document.createElement('span');
                                span.setAttribute('title',value);
                                span.innerHTML = value;
                                return span.outerHTML;
                            }},
                            {field: 'id', title: __('商品ID'), operate: 'LIKE'},
                            // {
                            //     field: 'buttons',
                            //     width: "120px",
                            //     title: __('修改SKU'),
                            //     table: table2,
                            //     events: Table.api.events.operate,
                            //     operate: false,
                            //     buttons: [
                            //         {
                            //             name: 'detail',
                            //             text: __('修改SKU'),
                            //             title: __('修改SKU'),
                            //             extend: 'data-area=\'["90%","90%"]\'',
                            //             classname: 'btn btn-sm btn-warning btn-dialog',
                            //             icon: 'fa fa-spotify',
                            //             url: 'products_shopify_variants/index'
                            //         },
                            //     ],
                            //     formatter: Table.api.formatter.buttons
                            // },
                            // {field: 'updated_at', title: __('Updated_at'), operate: 'LIKE'},
                            // {field: 'tags', title: __('Tags'), operate: 'LIKE'},
                            {field: 'images', title: __('主图'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.images},
                            // {field: 'options', title: __('Options'), operate: 'LIKE'},
                            // {field: 'status', title: __('Status'), operate: 'LIKE', formatter: Table.api.formatter.status},
                            // {field: 'operate', title: __('Operate'), table: table2, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                        ]
                    ]
                });

                // 为表格2绑定事件
                Table.api.bindevent(table2);
            }
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});